package com.yunxi.service.localService.impl.otd;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.core.util.BeanTools;
import com.yunxi.core.util.DateUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.dao.otd.OtdProduceCapacityMapper;
import com.yunxi.model.otd.OtdProduceCapacity;
import com.yunxi.model.otd.OtdProduceCapacityDTO;
import com.yunxi.otd.pojo.OtdCapacityChangeStateVO;
import com.yunxi.otd.pojo.OtdCapacityEditVO;
import com.yunxi.otd.pojo.OtdCapacityQueryVO;
import com.yunxi.otd.pojo.OtdCapacityRemoveVO;
import com.yunxi.otd.pojo.OtdCapacityVO;
import com.yunxi.service.localService.otd.OtdProduceCalendarService;
import com.yunxi.service.localService.otd.OtdProduceCapacityService;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 产能设置 服务实现类
 * </p>
 *
 * @author XUQI
 * @since 2019-08-26
 */
@Service
public class OtdProduceCapacityServiceImpl extends
    ServiceImplPlus<OtdProduceCapacityMapper, OtdProduceCapacity> implements OtdProduceCapacityService {

    @Autowired
    @Qualifier("otdProduceCalendarServiceImpl")
    OtdProduceCalendarService produceCalendarService;

    /**
     * 产能设置查询
     *
     * @param queryVO 查询条件
     * @return ApiResult
     */
    @Override
    public ApiResult findProduceCapacity(OtdCapacityQueryVO queryVO) {
        Map map = BeanTools.bean2map(queryVO);
        String year = queryVO.getYearMonth().substring(0, 4);
        String month = queryVO.getYearMonth().substring(4, 6);
        map.put("days", DateUtil.getDayListByMonth(Integer.valueOf(year), Integer.valueOf(month)));
        OtdCapacityVO data = new OtdCapacityVO();
        data.setTotal(baseMapper.selectProduceCapacityCount(map));
        data.setList(baseMapper.selectProduceCapacity(map));
        return ResponseUtil.ok(data);
    }


    /**
     * 产能数编辑
     *
     * @param editVO 编辑VO
     * @return ApiResult
     */
    @Override
    public ApiResult editCapacity(OtdCapacityEditVO editVO) {
        // 表达式、日期
        LambdaQueryWrapper<OtdProduceCapacity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OtdProduceCapacity::getExpression, editVO.getExpression())
            .eq(OtdProduceCapacity::getWorks, editVO.getWorks())
            .eq(OtdProduceCapacity::getProduceDate, editVO.getProduceDate());
        OtdProduceCapacity produceCapacity = baseMapper.selectOne(wrapper);
        if (produceCapacity == null) {
            produceCapacity = new OtdProduceCapacity();
            produceCapacity.setExpression(editVO.getExpression());
            produceCapacity.setDescription(editVO.getDescription());
            produceCapacity.setCapacity(editVO.getCapacity());
            produceCapacity.setPriority(editVO.getPriority());
            produceCapacity.setProduceDate(editVO.getProduceDate());
            produceCapacity.setWorks(editVO.getWorks());
            produceCapacity.setState(editVO.getState());
        }
        produceCapacity.setCapacity(editVO.getCapacity());
        this.saveOrUpdate(produceCapacity);
        return ResponseUtil.ok();
    }


    /**
     * 变更状态
     *
     * @param editVO 编辑VO
     * @return ApiResult
     */
    @Override
    public ApiResult changeState(OtdCapacityChangeStateVO editVO) {
        // 根据规则+工厂更新状态
        baseMapper
            .updatePriorityAndState(editVO.getYearMonth(), editVO.getExpression(), editVO.getWorks(), editVO.getState(),
                editVO.getPriority());
        return ResponseUtil.ok();
    }

    /**
     * 多条删除
     *
     * @param removeVOList 删除List
     * @return ApiResult
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeByVO(List<OtdCapacityRemoveVO> removeVOList) {
        if (CollectionUtils.isEmpty(removeVOList)) {
            return true;
        }
        for (OtdCapacityRemoveVO removeVO : removeVOList) {
            LambdaQueryWrapper<OtdProduceCapacity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OtdProduceCapacity::getExpression, removeVO.getExpression())
                .eq(OtdProduceCapacity::getWorks, removeVO.getWorks());
            super.remove(wrapper);
        }
        return true;
    }

    /**
     * 保存
     *
     * @param editVO 编辑VO
     * @return ApiResult
     */
    @Override
    public ApiResult saveOrUpdate(OtdCapacityEditVO editVO) {
        OtdProduceCapacity produceCapacity = new OtdProduceCapacity();
        BeanUtils.copyProperties(editVO, produceCapacity);
        saveOrUpdate(produceCapacity);
        return ResponseUtil.ok();
    }

    /**
     * 根据范围查询内工作日产能
     *
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return 产能
     */
    @Override
    public Map<Date, Map<String, List<OtdProduceCapacityDTO>>> selectWorkDayCapacity(Date startDate, Date endDate) {
        //// Map<日期，Map<条件表达式，List<产能设置对象> >>
        Map<Date, Map<String, List<OtdProduceCapacityDTO>>> capacityDateMap = new HashMap<>();

        // 根据范围查询内工作日产能
        List<OtdProduceCapacityDTO> capacityList = baseMapper.selectWorkDayCapacity(startDate, endDate);
        if (CollectionUtils.isEmpty(capacityList)) {
            return null;
        }
        // 根据约束条件分组
        for (OtdProduceCapacityDTO capacity : capacityList) {
            //// 1.产能条件 key:条件 value:产能条件设置对象
            Map<String, List<OtdProduceCapacityDTO>> capacityMap = capacityDateMap.get(capacity.getDay());
            if (CollectionUtils.isEmpty(capacityMap)) {
                capacityMap = new HashMap<>();
            }
            List<OtdProduceCapacityDTO> otdProduceCapacityDTOS = capacityMap.get(capacity.getExpression());
            if (CollectionUtils.isEmpty(otdProduceCapacityDTOS)) {
                otdProduceCapacityDTOS = new ArrayList<>();
            }
            otdProduceCapacityDTOS.add(capacity);
            capacityMap.put(capacity.getExpression(), otdProduceCapacityDTOS);
            capacityDateMap.put(capacity.getDay(), capacityMap);
        }
        return capacityDateMap;
    }

    /**
     * 校验是否为一个数字
     *
     * @param num 日
     * @return 是 true 否 false
     */
    private boolean checkNumber(String num) {
        Pattern pattern = Pattern.compile("^\\d+$");
        return pattern.matcher(num).matches();
    }

}
